﻿using StrategyMode.Discount;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace StrategyMode
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private double sumPrice = 0d;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //根据DiscountType 遍历枚举数据
            Dictionary<string, int> types = new Dictionary<string, int>();
            foreach (var item in Enum.GetValues(typeof(DiscountType)))
            {
                types.Add(item.ToString(), Convert.ToInt32(item));
            }
            //绑定到下拉菜单
            combobox_DiscountType.ItemsSource = types;
            combobox_DiscountType.DisplayMemberPath = "Key";
            combobox_DiscountType.SelectedValuePath = "Value";
        }

        private void button_OK_Click(object sender, RoutedEventArgs e)
        {
            //获取用户设置的单价和数量
            double originalUnitPrice = 0;
            double.TryParse(textbox_UnitPrice.Text, out originalUnitPrice);
            int originalNumber = 0;
            int.TryParse(textbox_Number.Text, out originalNumber);
            var original = originalUnitPrice * originalNumber;
            if (original <= 0)
                return;
            if (combobox_DiscountType.SelectedIndex == -1)
                return;
            //根据下拉的折扣、产品的单价、产品的数量获得实际费用
            DiscountContext context = new DiscountContext((DiscountType)combobox_DiscountType.SelectedValue);
            var result = context.GetResult(original);
            textbox_OrderList.Text += $"单价：{originalUnitPrice}；数量：{originalNumber}；消费金额：{result}\r";
            //获取总计
            sumPrice += result;
            textblock_Sum.Text = sumPrice.ToString();
        }
    }
}
